﻿// ================================================================================
//        File: SystemLibrary.cs
//        Desc: 系统各种可共同调用方法
//        Auth: Alex
//        Date: 2010-09-17
// ================================================================================
//        Change History
// ================================================================================
//        Date:         Author:                  Description:
//        --------     --------              -------------------
//              
// ================================================================================


using System;
using System.Collections.Generic;
using System.Text;

namespace EShop.Common
{
    public class SystemLibrary
    {
        /// <summary>
        /// SQL搜索时要将字符中的特殊字符替换
        /// </summary>
        /// <param name="sInputString"></param>
        /// <returns></returns>
        public static string SQLQuoteName(string sInputString)
        {
            string sOutputString = null;
            sOutputString = sInputString.Replace("'", "''");
            sOutputString = sOutputString.Replace("[", "[[]");
            sOutputString = sOutputString.Replace("%", "[%]");
            sOutputString = sOutputString.Replace("_", "[_]");
            return sOutputString;
        }

        /// <summary>
        /// 系统统一编码为GB2312
        /// JF
        /// </summary>
        public static Encoding SystemEncode
        {
            get
            {
                Encoding enc = Encoding.GetEncoding("gb2312");
                return enc;
            }
        }

        /// <summary>
        /// 将相应的int值转换成相应的权限
        /// </summary>
        /// <param name="strValue"></param>
        /// <returns>相应的权限</returns>
        public static PermissionEnum IntConvertToPermission(string strValue)
        {
            return (PermissionEnum)Enum.Parse(typeof(PermissionEnum), strValue);
        }

        /// <summary>
        /// 将用户当前权限转换为Int
        /// </summary>
        /// <param name="userPermission"></param>
        /// <returns></returns>
        public static int PermissionToInt(PermissionEnum userPermission)
        {
            return (int)userPermission;
        }

        /// <summary>
        /// 添加用户多个权限
        /// </summary>
        /// <param name="userPermission">用户现有权限</param>
        /// <param name="permissionFlag">要添加的权限</param>
        /// <returns></returns>
        public static PermissionEnum AddPermission(PermissionEnum userPermission, PermissionEnum permissionFlag)
        {
            return userPermission | permissionFlag;
        }

        /// <summary>
        /// 移除相应的权限
        /// </summary>
        /// <param name="userPermission">用户现有权限</param>
        /// <param name="removeFlag">要移除的权限</param>
        /// <returns></returns>
        public static PermissionEnum removePermission(PermissionEnum userPermission, PermissionEnum removeFlag)
        {
            if ((userPermission & removeFlag) != 0)
                return userPermission ^ removeFlag;
            return userPermission;            
        }
    }
}
